// pages/index/index.js
import request from '../../utils/request'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bannerList: [],
    recommendList: [],
    topList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    //轮播图
   let bannerData = await request('/banner', {type: 2})
      this.setData({
        bannerList: bannerData.banners
      
    })
    //推荐歌单
   let recommendData =  await request('/personalized', {limit: 20})
     this.setData({
      recommendList: recommendData.result
     
   })
   //排行榜 
   let index = 0;
   let resultList = [];
   while(index < 5) {
    let topListData =  await request('/top/list?idx=6', {idx: index++})
    let topListItem = {name: topListData.playlist.name, tracks: topListData.playlist.tracks.slice(0, 3)}
    resultList.push(topListItem)
    //放这里用户体验更好，但渲染次数变多
    this.setData({
      topList: resultList
    })
   }
  
  },
  toRecommendSong() {
    wx.navigateTo({
      url: '/songPacket/pages/RecommendSong/RecommendSong',
    });
  },
  toOther() {
    wx.navigateTo({
      url: '/otherPacket/pages/other/other',
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    return {
      title: '大头的分享',
      page: '/pages/index/index'
    }
   
  }
})